﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Linq;
using System.Text;
using System.Windows;
using Vtron.HipcConfigurationTools.Core;
using Vtron.HipcConfigurationTools.Domain;

namespace Vtron.HipcConfigurationTools.VWASStyle
{
    /// <summary>
    /// SetNetConfigWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetNetConfigWindow : ModernWindow
    {
        private NetworkInfo networkInfo;

        public SetNetConfigWindow(NetworkInfo networkInfo)
        {
            InitializeComponent();

            this.Style = (Style)App.Current.Resources["EmptyWindow"];
            if (networkInfo == null)
            {
                return;
            }

            this.networkInfo = networkInfo;
            isDHCP_CheckBox.IsChecked = networkInfo.IsDHCP;
            ip_TextBox.Text = networkInfo.Ip;
            mask_TextBox.Text = networkInfo.Mask;
            gateway_TextBox.Text = networkInfo.Gateway;
            mac_TextBox.Text = networkInfo.Mac;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            SocketHandler handler = new SocketHandler();
            T_HIPC_NET_CONFIG_REQ request = new T_HIPC_NET_CONFIG_REQ();
            request.head = new T_HIPC_NET_HEADER() { msgType = 0x840a };
            request.netInfo = new T_HIPC_NET_INFO();
            request.dhcp = (bool)isDHCP_CheckBox.IsChecked ? (byte)1 : (byte)0;
            request.netInfo.ipAddress = Encoding.Default.GetBytes(ip_TextBox.Text.Trim());
            request.netInfo.ipAddress = request.netInfo.ipAddress.Concat(new byte[20 - request.netInfo.ipAddress.Length]).ToArray();
            request.netInfo.netMask = Encoding.Default.GetBytes(mask_TextBox.Text.Trim());
            request.netInfo.netMask = request.netInfo.netMask.Concat(new byte[20 - request.netInfo.netMask.Length]).ToArray();
            request.netInfo.gatway = Encoding.Default.GetBytes(gateway_TextBox.Text.Trim());
            request.netInfo.gatway = request.netInfo.gatway.Concat(new byte[20 - request.netInfo.gatway.Length]).ToArray();
            request.netInfo.macAddress = Encoding.Default.GetBytes(mac_TextBox.Text.Trim());
            request.netInfo.macAddress = request.netInfo.macAddress.Concat(new byte[20 - request.netInfo.macAddress.Length]).ToArray();
            MainWindowViewModel mainWindowViewModel = App.Current.MainWindow.DataContext as MainWindowViewModel;
            T_HIPC_NET_CONFIG_RESP response;
            try
            {
                response = handler.Handle<T_HIPC_NET_CONFIG_RESP>(request);
            }
            catch
            {
                try
                {
                    Utils.IP = ip_TextBox.Text.Trim();
                    mainWindowViewModel.SearchNetConfig();
                    if (mainWindowViewModel.NetworkInfo.Mac == mac_TextBox.Text.Trim() &&
                        mainWindowViewModel.NetworkInfo.Ip == ip_TextBox.Text.Trim() &&
                        mainWindowViewModel.NetworkInfo.Mask == mask_TextBox.Text.Trim() &&
                        mainWindowViewModel.NetworkInfo.Gateway == gateway_TextBox.Text.Trim())
                    {
                        ModernDialog.ShowMessage("网络设置成功", "信息", MessageBoxButton.OK);
                        //mainWindowViewModel.NewIp = ip_TextBox.Text.Trim();
                        Close();
                    }
                    else
                    {
                        Utils.IP = networkInfo.Ip;
                        ModernDialog.ShowMessage("网络设置失败！原因：修改网络参数命令返回了正确的应答，但实际的网络参数并没有修改", "信息", MessageBoxButton.OK);
                    }
                    return;
                }
                catch
                {
                    Utils.IP = networkInfo.Ip;
                    ModernDialog.ShowMessage("网络设置失败！", "信息", MessageBoxButton.OK);

                    return;
                }
            }
            if (response.head.msgType == 0x840b)
            {
                if (response.head.errCode == 0)
                {
                    string orgIp = Utils.IP;
                    try
                    {
                        Utils.IP = ip_TextBox.Text.Trim();
                        ModernDialog.ShowMessage("网络设置成功，需要板卡复位后才能生效！", "信息", MessageBoxButton.OK);

                        //mainWindowViewModel.SearchNetConfig();
                        //if (mainWindowViewModel.NetworkInfo.IsDHCP == (bool)isDHCP_CheckBox.IsChecked &&
                        //    mainWindowViewModel.NetworkInfo.Mac == mac_TextBox.Text.Trim() &&
                        //    mainWindowViewModel.NetworkInfo.Ip == ip_TextBox.Text.Trim() &&
                        //    mainWindowViewModel.NetworkInfo.Mask == mask_TextBox.Text.Trim() &&
                        //    mainWindowViewModel.NetworkInfo.Gateway == gateway_TextBox.Text.Trim())
                        //{
                        //    ModernDialog.ShowMessage("网络设置成功", "信息", MessageBoxButton.OK);
                        //    mainWindowViewModel.NewIp = ip_TextBox.Text.Trim();
                        //    Close();
                        //}
                        //else
                        //{
                        //    Utils.IP = orgIp;
                        //    ModernDialog.ShowMessage("网络设置失败！原因：修改网络参数命令返回了正确的应答，但实际的网络参数并没有修改", "信息", MessageBoxButton.OK);
                        //}
                    }
                    catch
                    {
                        Utils.IP = orgIp;
                        ModernDialog.ShowMessage("网络设置失败！", "信息", MessageBoxButton.OK);

                        return;
                    }
                }
                else
                {
                    ModernDialog.ShowMessage("网络设置失败！", "信息", MessageBoxButton.OK);
                }
            }
        }
    }
}
